package com.dlc.shop.allinpay.service;

import com.alibaba.fastjson.JSON;
import com.dlc.shop.allinpay.OpenClient;
import com.dlc.shop.allinpay.config.AllinpayConfig;
import com.dlc.shop.bean.enums.AuditStatus;
import com.dlc.shop.common.allinpay.member.req.*;
import com.dlc.shop.common.allinpay.member.resp.*;
import com.dlc.shop.common.exception.YamiShopBindException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author LGH
 */
@Component
@AllArgsConstructor
public class AllinpayMemberService {

    private final AllinpayConfig allinpayConfig;

    /**
     * @param cm 创建会员
     */
    public void createMember(CreateMember cm) {
        allinpayConfig.sendCreateData(cm);
    }

    /**
     * @param svc 绑定手机号 - 发送短信验证码
     */
    public void sendVerificationCode(SendVerificationCode svc) {
        allinpayConfig.sendData(svc, PhoneVerificationResp.class);
    }

    /**
     * @param bindPhone 绑定手机
     */
    public void bindPhone(BindPhone bindPhone) {
        allinpayConfig.sendData(bindPhone, PhoneVerificationResp.class);
    }

    /**
     * @param unbindPhone 解绑手机
     * @return
     */
    public PhoneVerificationResp unbindPhone(UnbindPhone unbindPhone) {
        return allinpayConfig.sendData(unbindPhone, PhoneVerificationResp.class);
    }

    /**
     * 修改绑定手机
     *
     * @param updatePhoneByPayPwd
     * @return
     */
    public String updatePhoneByPayPwd(UpdatePhoneByPayPwd updatePhoneByPayPwd) {
        return allinpayConfig.sendUrlData(updatePhoneByPayPwd);
    }

    /**
     * @param setRealName 个人实名认证
     */
    public void setRealName(SetRealName setRealName) {
        allinpayConfig.sendData(setRealName, SetRealNameResp.class);
    }

    /**
     * @param setCompanyInfo 设置企业信息
     * @return
     */
    public SetCompanyInfoResp setCompanyInfo(SetCompanyInfo setCompanyInfo) {
        SetCompanyInfoResp setCompanyInfoResp = new SetCompanyInfoResp();
        try {
            setCompanyInfoResp = allinpayConfig.sendData(setCompanyInfo, SetCompanyInfoResp.class);
        } catch (YamiShopBindException e) {
            setCompanyInfoResp.setResult(AuditStatus.FAIL.value());
            setCompanyInfoResp.setFailReason(e.getMessage());
        }
        return setCompanyInfoResp;
    }

    /**
     * 影印件采集
     *
     * @param idCardCollect
     * @return
     */
    public IdCardCollectResp idCardCollect(IdCardCollect idCardCollect) {
        return allinpayConfig.sendData(idCardCollect, IdCardCollectResp.class);
    }

    /**
     * 影印件采集（文件上传模式）
     *
     * @param idCardCollectByFileUpload
     * @return
     */
    public IdCardCollectByFileUploadResp idcardCollectByFileUpload(IdCardCollectByFileUpload idCardCollectByFileUpload) {
        return allinpayConfig.sendData(idCardCollectByFileUpload, IdCardCollectByFileUploadResp.class);
    }

    /**
     * @param updateCompanyInfo 修改企业信息
     * @return
     */
    public UpdateCompanyInfoResp updateCompanyInfo(UpdateCompanyInfo updateCompanyInfo) {
        return allinpayConfig.sendData(updateCompanyInfo, UpdateCompanyInfoResp.class);
    }


    /**
     * 获取个人会员信息
     *
     * @return
     */
    public UserInfo getMemberInfo(GetMemberInfo getMemberInfo) {
        MemberInfo memberInfo = allinpayConfig.sendData(getMemberInfo, MemberInfo.class);
        return JSON.parseObject(memberInfo.getMemberInfo(), UserInfo.class);
    }

    /**
     * 获取企业用户信息
     */
    public CompanyInfo getCompanyInfo(GetMemberInfo getMemberInfo) {
        MemberInfo memberInfo = allinpayConfig.sendData(getMemberInfo, MemberInfo.class);
        return JSON.parseObject(memberInfo.getMemberInfo(), CompanyInfo.class);
    }

    /**
     * 查询卡bin
     */
    public CardBinInfo getBankCardBin(GetBankCardBin getBankCardBin) {
        BankCardBin bankCardBin = allinpayConfig.sendData(getBankCardBin, BankCardBin.class);
        return JSON.parseObject(bankCardBin.getCardBinInfo(), CardBinInfo.class);
    }

    /**
     * 绑定银行卡
     */
    public ApplyBindBankCardResp applyBindBankCard(ApplyBindBankCard applyBindBankCard) {
        return allinpayConfig.sendData(applyBindBankCard, ApplyBindBankCardResp.class);
    }

    /**
     * 确认绑定银行卡
     */
    public void bindBankCard(BindBankCard bindBankCard) {
        allinpayConfig.sendData(bindBankCard, BindBankCardResp.class);
    }

    /**
     * 获取用户所绑定的银行卡
     */
    public List<BindCard> queryBankCard(QueryBankCard queryBankCard) {
        QueryBankCardResp queryBankCardResp = allinpayConfig.sendData(queryBankCard, QueryBankCardResp.class);
        // 卡号解密
        List<BindCard> bindCards = queryBankCardResp.getBindCardList();
        OpenClient client = allinpayConfig.getClient();
        for (BindCard bindCard : bindCards) {
            bindCard.setBankCardNo(client.decrypt(bindCard.getBankCardNo()));
        }
        return bindCards;
    }

    /**
     * 解除绑定银行卡
     */
    public void unbindBankCard(UnbindBankCard unbindBankCard) {
        allinpayConfig.sendData(unbindBankCard, CardNoWithUid.class);
    }

    /**
     * 锁定会员
     */
    public void lockMember(LockMember lockMember) {
        allinpayConfig.sendData(lockMember, BizUserId.class);
    }

    /**
     * 解锁会员
     */
    public void unlockMember(UnlockMember unlockMember) {
        allinpayConfig.sendData(unlockMember, BizUserId.class);
    }

    /**
     * @param applyBindAcct 会员绑定支付账户用户标识
     * @return
     */
    public Result applyBindAcct(ApplyBindAcct applyBindAcct) {
        return allinpayConfig.sendData(applyBindAcct, Result.class);
    }

    /**
     * 查询会员绑定支付账户用户标识
     *
     * @param applyBindAcct
     * @return
     */
    public QueryBindAcctResp queryBindAcct(ApplyBindAcct applyBindAcct) {
        return allinpayConfig.sendData(applyBindAcct, QueryBindAcctResp.class);
    }

    /**
     * @param setPayPwd 设置支付密码
     * @return
     */
    public String setPayPwd(SetPayPwd setPayPwd) {
        return allinpayConfig.sendUrlData(setPayPwd);
    }

    /**
     * @param updatePayPwd 设置支付密码
     * @return
     */
    public String updatePayPwd(UpdatePayPwd updatePayPwd) {
        return allinpayConfig.sendUrlData(updatePayPwd);
    }

    /**
     * @param resetPayPwd 重置支付密码
     * @return
     */
    public String resetPayPwd(ResetPayPwd resetPayPwd) {
        return allinpayConfig.sendUrlData(resetPayPwd);
    }

    /**
     * 企业会员绑定对公户
     *
     * @param bindCompanyAccount
     */
    public void bindCompanyAccount(BindCompanyAccount bindCompanyAccount) {
        allinpayConfig.sendData(bindCompanyAccount, BizUserId.class);
    }

    /**
     * 账户提现协议签约
     *
     * @param signAcctProtocol
     * @return
     */
    public String signAcctProtocol(SignAcctProtocol signAcctProtocol) {
        return allinpayConfig.sendUrlData(signAcctProtocol);
    }

    /**
     * 账户协议签约查询
     *
     * @param signContractQuery
     * @return
     */
    public String signContractQuery(SignContractQuery signContractQuery) {
        return allinpayConfig.sendUrlData(signContractQuery);
    }

    /**
     * 修改绑定手机（银行卡验证）
     *
     * @param bankCardChangeBindPhone
     * @return
     */
    public BankCardChangeBindPhoneResp bankCardChangeBindPhone(BankCardChangeBindPhone bankCardChangeBindPhone) {
        return allinpayConfig.sendData(bankCardChangeBindPhone, BankCardChangeBindPhoneResp.class);
    }

    /**
     * 确认修改绑定手机（银行卡验证）
     *
     * @param verifyBankCardChangeBindPhone
     */
    public void verifyBankCardChangeBindPhone(VerifyBankCardChangeBindPhone verifyBankCardChangeBindPhone) {
        allinpayConfig.sendData(verifyBankCardChangeBindPhone, VerifyBankCardChangeBindPhoneResp.class);
    }
}
